#!/bin/bash

# PGM平台快速测试脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_message() {
    echo -e "${2}${1}${NC}"
}

print_message "=== PGM平台功能测试 ===" $BLUE

# 检查PGM是否在运行
check_pgm_running() {
    print_message "检查PGM服务状态..." $BLUE
    
    # 检查8080端口是否被占用
    if curl -s http://localhost:8080 > /dev/null 2>&1; then
        print_message "✓ PGM服务正在运行" $GREEN
        return 0
    else
        print_message "✗ PGM服务未运行，请先启动服务" $RED
        print_message "提示: 运行 './scripts/start.sh --db your_database' 启动服务" $YELLOW
        return 1
    fi
}

# 测试API接口
test_api() {
    print_message "测试API接口..." $BLUE
    
    # 测试获取表列表
    print_message "1. 测试获取表列表API..." $YELLOW
    response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/api/tables)
    if [ "$response" = "200" ]; then
        print_message "   ✓ 表列表API正常" $GREEN
    else
        print_message "   ✗ 表列表API异常 (HTTP $response)" $RED
    fi
    
    # 获取具体的表列表
    tables=$(curl -s http://localhost:8080/api/tables | grep -o '"name":"[^"]*"' | cut -d'"' -f4 | head -3)
    
    if [ -n "$tables" ]; then
        print_message "   发现的表: $(echo $tables | tr '\n' ' ')" $GREEN
        
        # 测试第一个表的数据
        first_table=$(echo $tables | head -1)
        if [ -n "$first_table" ]; then
            print_message "2. 测试数据查询API (表: $first_table)..." $YELLOW
            response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:8080/api/data/$first_table?page=1&page_size=5")
            if [ "$response" = "200" ]; then
                print_message "   ✓ 数据查询API正常" $GREEN
            else
                print_message "   ✗ 数据查询API异常 (HTTP $response)" $RED
            fi
        fi
    else
        print_message "   ⚠ 未发现数据表，请检查数据库连接" $YELLOW
    fi
}

# 测试Web界面
test_web() {
    print_message "测试Web界面..." $BLUE
    
    # 测试首页
    print_message "1. 测试首页..." $YELLOW
    response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/)
    if [ "$response" = "200" ]; then
        print_message "   ✓ 首页加载正常" $GREEN
    else
        print_message "   ✗ 首页加载异常 (HTTP $response)" $RED
    fi
    
    # 测试静态资源
    print_message "2. 测试静态资源..." $YELLOW
    css_response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/static/css/app.css)
    js_response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/static/js/app.js)
    
    if [ "$css_response" = "200" ] && [ "$js_response" = "200" ]; then
        print_message "   ✓ 静态资源加载正常" $GREEN
    else
        print_message "   ✗ 静态资源加载异常 (CSS: $css_response, JS: $js_response)" $RED
    fi
}

# 性能测试
test_performance() {
    print_message "简单性能测试..." $BLUE
    
    # 测试API响应时间
    print_message "测试API响应时间..." $YELLOW
    time_result=$(curl -s -o /dev/null -w "%{time_total}" http://localhost:8080/api/tables)
    print_message "   表列表API响应时间: ${time_result}秒" $GREEN
}

# 生成测试报告
generate_report() {
    print_message "生成测试报告..." $BLUE
    
    report_file="test_report_$(date +%Y%m%d_%H%M%S).txt"
    
    {
        echo "PGM平台测试报告"
        echo "================"
        echo "测试时间: $(date)"
        echo "测试环境: $(uname -a)"
        echo ""
        echo "服务状态:"
        curl -s http://localhost:8080/api/tables | head -5
        echo ""
        echo "系统信息:"
        echo "- Go版本: $(go version 2>/dev/null || echo '未安装')"
        echo "- 操作系统: $(uname -s)"
        echo "- 内存使用: $(free -h 2>/dev/null | grep 'Mem:' || echo '无法获取')"
        echo ""
    } > "$report_file"
    
    print_message "测试报告已保存到: $report_file" $GREEN
}

# 显示使用建议
show_suggestions() {
    print_message "使用建议:" $BLUE
    echo "1. 浏览器访问: http://localhost:8080"
    echo "2. API文档: http://localhost:8080/api/tables"
    echo "3. 如需自定义配置，编辑 configs/config.yaml"
    echo "4. 查看日志以了解系统状态"
    echo "5. 使用 Ctrl+C 停止服务"
}

# 主测试流程
main() {
    if ! check_pgm_running; then
        exit 1
    fi
    
    test_api
    test_web
    test_performance
    
    print_message "=== 测试完成 ===" $GREEN
    show_suggestions
    
    # 询问是否生成详细报告
    read -p "是否生成详细测试报告? (y/n): " generate
    if [ "$generate" = "y" ] || [ "$generate" = "Y" ]; then
        generate_report
    fi
}

# 检查命令行参数
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
    echo "PGM平台测试脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  --help, -h    显示帮助信息"
    echo ""
    echo "说明:"
    echo "  此脚本会测试PGM平台的基本功能，包括API接口和Web界面"
    echo "  运行前请确保PGM服务已启动"
    echo ""
    exit 0
fi

main